Skip to content

feat: allow AS-bound variable references#142

Merged
jitsedesmet merged 6 commits into
mainfrom
fix/fix-reuse-of-vars
Jun 3, 2026
Merged

feat: allow AS-bound variable references#142
jitsedesmet merged 6 commits into
mainfrom
fix/fix-reuse-of-vars

Conversation

@jitsedesmet

Copy link
Copy Markdown
Member

Add support for referencing variables bound by preceding (expr AS ?var) expressions in subsequent SELECT expressions, per the updated SPARQL spec (w3c/sparql-query#380, w3c/rdf-tests#340).

The query SELECT (COUNT(?v) AS ?count) (?count + 1 AS ?countPlusOne) ... was previously rejected with 'Use of ungrouped variable' because the validator did not recognize that ?count was in scope from an earlier AS binding.

Changes:

  • Fix queryProjectionIsGood validation to track AS-bound variables and allow their use in subsequent projection expressions
  • Add select-variable-reuse test with AST, algebra, and generator statics

…rences

Add support for referencing variables bound by preceding (expr AS ?var)
expressions in subsequent SELECT expressions, per the updated SPARQL spec
(w3c/sparql-query#380, w3c/rdf-tests#340).

The query `SELECT (COUNT(?v) AS ?count) (?count + 1 AS ?countPlusOne) ...`
was previously rejected with 'Use of ungrouped variable' because the
validator did not recognize that ?count was in scope from an earlier
AS binding.

Changes:
- Fix queryProjectionIsGood validation to track AS-bound variables and
  allow their use in subsequent projection expressions
- Add select-variable-reuse test with AST, algebra, and generator statics

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@jitsedesmet

Copy link
Copy Markdown
Member Author

@rubensworks should this be changed in the 1.1 parser also? Or is this 1.2 specific?

@rubensworks

Copy link
Copy Markdown
Member

1.2 is fine.

jitsedesmet and others added 5 commits May 27, 2026 09:04
The queryProjectionIsGood function in rules-sparql-1-2/validators.ts is a
separate implementation from the 1.1 version (changed per w3c/sparql-query#380).
Although all 1.1 tests correctly run against the 1.2 parser, several branches
of this function were not reached by any existing test.

Fix by adding cases to the shared test infrastructure so they run on
both parsers automatically:

- 3 new sparql-1-1-invalid static queries (select-star-with-group-by,
  ungrouped-variable-in-expression-projection,
  count-with-ungrouped-expression-variable) that cover the throw paths
  in queryProjectionIsGood (lines 123 and 158).

- 3 new note tests in importSparql11NoteTests covering the no-throw
  branches: AS variable not conflicting with a term-var subquery,
  AS variable not conflicting with a wildcard subquery, and expression
  projection variable covered by GROUP BY (line 158 and 183-186 FALSE
  branches).

Also add rapport.md documenting the investigation findings.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Replace the three programmatic 'should NOT throw' tests added in the
previous commit with proper static test files that slot into the shared
positive-test infrastructure and therefore run automatically on all
parsers (1.1, 1.1-adjust, 1.2) and the 1.2 generator.

Files added per test case:
- sparql/sparql-1-1/<name>.sparql        – the query under test
- ast-source-tracked/sparql-1-1/<name>.json – expected AST (source-tracked)
- sparql-generated/sparql-1-1/<name>.sparql – expected generator output
- sparql-generated-compact/sparql-1-1/<name>.sparql – compact generator output

Cases migrated:
- note-as-var-not-in-term-subquery
- note-as-var-not-in-wildcard-subquery
- note-expression-grouped-var

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@jitsedesmet jitsedesmet marked this pull request as ready for review June 3, 2026 14:28
@jitsedesmet jitsedesmet merged commit bbe54eb into main Jun 3, 2026
55 checks passed
@jitsedesmet jitsedesmet deleted the fix/fix-reuse-of-vars branch June 3, 2026 14:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants